package com.ruoyi.web.controller.merchantStatistics;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.merchant.domain.MpMerchant;
import com.ruoyi.merchant.domain.MpMerchantRSP;
import com.ruoyi.merchant.service.IMpMerchantService;
import com.ruoyi.merchantStatistics.domain.MerchantStatisticsWithdrawal;
import com.ruoyi.merchantStatistics.domain.MerchantStatisticsWithdrawalExport;
import com.ruoyi.merchantStatistics.service.IMerchantStatisticsWithdrawalService;
import com.ruoyi.statistics.domain.StatisticsWithdrawal;
import com.ruoyi.statistics.domain.StatisticsWithdrawalExport;
import com.ruoyi.statistics.service.IStatisticsWithdrawalService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-07
 */
@Controller
@RequestMapping("/merchantStatistics/statisticsWithdrawal")
public class MerchantStatisticsWithdrawalController extends BaseController
{
    private String prefix = "merchantStatistics/statisticsWithdrawal";
	
	@Autowired
	private IMerchantStatisticsWithdrawalService statisticsWithdrawalService;
	@Autowired
	private IMpMerchantService mpMerchantService;
	
	@RequiresPermissions("merchantStatistics:statisticsWithdrawal:view")
	@GetMapping()
	public String statisticsWithdrawal()
	{
	    return prefix + "/statisticsWithdrawal";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("merchantStatistics:statisticsWithdrawal:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(MerchantStatisticsWithdrawal statisticsWithdrawal)
	{
		MpMerchant mpMerchant = new MpMerchant();
		mpMerchant.setUserId(ShiroUtils.getUserId().intValue());
		List<MpMerchantRSP> mpMerchantRSPS = mpMerchantService.selectMpMerchantList(mpMerchant);
		if(mpMerchantRSPS.size() > 0){
			MpMerchantRSP mpMerchantRSP = mpMerchantRSPS.get(0);
			statisticsWithdrawal.setMerchantId(mpMerchantRSP.getId());
		}else {
			statisticsWithdrawal.setMerchantId(0L);
		}
		if (StringUtils.isNull(statisticsWithdrawal.getChannelName())) {
			statisticsWithdrawal.setChannelName("");
		}
		startPage();
        List<MerchantStatisticsWithdrawal> list = statisticsWithdrawalService.selectStatisticsWithdrawalList(statisticsWithdrawal);
		return getDataTable(list);
	}



	
	/**
	 * 导出列表
	 */
	@RequiresPermissions("merchantStatistics:statisticsWithdrawal:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MerchantStatisticsWithdrawal statisticsWithdrawal)
    {
		MpMerchant mpMerchant = new MpMerchant();
		mpMerchant.setUserId(ShiroUtils.getUserId().intValue());
		List<MpMerchantRSP> mpMerchantRSPS = mpMerchantService.selectMpMerchantList(mpMerchant);
		if(mpMerchantRSPS.size() > 0){
			MpMerchantRSP mpMerchantRSP = mpMerchantRSPS.get(0);
			statisticsWithdrawal.setMerchantId(mpMerchantRSP.getId());
		}else {
			statisticsWithdrawal.setMerchantId(0L);
		}
    	List<MerchantStatisticsWithdrawal> list = statisticsWithdrawalService.selectStatisticsWithdrawalList(statisticsWithdrawal);
		List<MerchantStatisticsWithdrawalExport> withdrawalExports = dataConvert(list);
		ExcelUtil<MerchantStatisticsWithdrawalExport> util = new ExcelUtil<MerchantStatisticsWithdrawalExport>(MerchantStatisticsWithdrawalExport.class);
        return util.exportExcel(withdrawalExports, "statisticsWithdrawal");
    }



	/**
	 * 数据转换
	 */
	public List<MerchantStatisticsWithdrawalExport> dataConvert(List<MerchantStatisticsWithdrawal> list) {
		List<MerchantStatisticsWithdrawalExport> withdrawalExportList = new ArrayList<>();
		for (MerchantStatisticsWithdrawal statisticsWithdrawal : list) {
			MerchantStatisticsWithdrawalExport statisticsWithdrawalExport = new MerchantStatisticsWithdrawalExport();
			BeanUtils.copyProperties(statisticsWithdrawal, statisticsWithdrawalExport);
			BigDecimal yuan = new BigDecimal("100");
			// 预期代付额
			statisticsWithdrawalExport.setExpectFee(statisticsWithdrawal.getExpectFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));

			// 预期商户出账
			statisticsWithdrawalExport.setExpectMchFee(statisticsWithdrawal.getExpectMchFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));

			// 实际代付额
			statisticsWithdrawalExport.setRealFee(statisticsWithdrawal.getRealFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));

			// 实际商户出账
			statisticsWithdrawalExport.setRealMchFee(statisticsWithdrawal.getRealMchFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));


			// 统计类型
			if (statisticsWithdrawal.getStatisticsType() == 1) {
				statisticsWithdrawalExport.setType("天");
			}else if (statisticsWithdrawal.getStatisticsType() == 2) {
				statisticsWithdrawalExport.setType("小时");
			}

			withdrawalExportList.add(statisticsWithdrawalExport);
		}
		return withdrawalExportList;
	}





	/**
	 * 新增
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@RequiresPermissions("merchantStatistics:statisticsWithdrawal:add")
	@Log(title = "", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(MerchantStatisticsWithdrawal statisticsWithdrawal)
	{		
		return toAjax(statisticsWithdrawalService.insertStatisticsWithdrawal(statisticsWithdrawal));
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		MerchantStatisticsWithdrawal statisticsWithdrawal = statisticsWithdrawalService.selectStatisticsWithdrawalById(id);
		mmap.put("statisticsWithdrawal", statisticsWithdrawal);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@RequiresPermissions("merchantStatistics:statisticsWithdrawal:edit")
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MerchantStatisticsWithdrawal statisticsWithdrawal)
	{		
		return toAjax(statisticsWithdrawalService.updateStatisticsWithdrawal(statisticsWithdrawal));
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("merchantStatistics:statisticsWithdrawal:remove")
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(statisticsWithdrawalService.deleteStatisticsWithdrawalByIds(ids));
	}
	
}
